//
//  LocalSave.m
//  个人项目-汽车
//
//  Created by 卢佳威 on 15/8/26.
//  Copyright (c) 2015年 卢佳威. All rights reserved.
//

#import "LocalSave.h"

@implementation LocalSave

// 获取文件路径
+ (NSString *)filePath:(NSString *)fileName
{
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *str = [arr firstObject];
    NSString *path = [NSString stringWithFormat:@"%@/%@", str, fileName];
    return path;
}

// 存储文件
+ (void)saveFile:(NSDictionary *)dic fileName:(NSString *)fileName
{
    NSString *path = [LocalSave filePath:fileName];
    NSLog(@"path ===============, %@", path);
    [dic writeToFile:path atomically:YES];
}
+ (void)saveFileOfArr:(NSArray *)arr fileName:(NSString *)fileName{
    NSString *path = [LocalSave filePath:fileName];
    NSLog(@"path ===============, %@", path);
    [arr writeToFile:path atomically:YES];
}

//存入data类型文件
+ (void) saveFileData:(NSData *)data fileName:(NSString *)fileName
{
    NSString *path = [LocalSave filePath:fileName];
    NSLog(@"path ===============, %@", path);
    [data writeToFile:path atomically:YES];

}

//读取data类型文件
+ (NSData *)readDataFile:(NSString *)fileName
{
    if ([LocalSave judgeFileExists:fileName]) {
        NSString *path = [LocalSave filePath:fileName];
        NSData *data = [NSData dataWithContentsOfFile:path];
        return data;
    }
    return [NSData data];

}

// 读取文件
+ (NSDictionary *)readFile:(NSString *)fileName
{
    if ([LocalSave judgeFileExists:fileName]) {
        NSString *path = [LocalSave filePath:fileName];
        NSDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
        return dic;
    }
    return [NSDictionary dictionary];
}

+ (NSArray *)readFileArr:(NSString *)fileName{
    if ([LocalSave judgeFileExists:fileName]) {
        NSString *path = [LocalSave filePath:fileName];
        NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:path];
        return arr;
    }
    return [NSArray array];
}

// 判断文件是否存在
+ (BOOL)judgeFileExists:(NSString *)fileName
{
    NSString *path = [LocalSave filePath:fileName];
    NSFileManager *manage = [NSFileManager defaultManager];
    return [manage fileExistsAtPath:path];
}

@end
